package cz.muni.fi.pb138.videodes.impl;

import cz.muni.fi.pb138.videodes.entity.Dvd;
import cz.muni.fi.pb138.videodes.entity.Genre;
import cz.muni.fi.pb138.videodes.entity.Movie;
import java.io.File;
import java.util.List;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.doc.table.OdfTable;

/**
 * This class is for exporting dvds to other XMLs.
 * 
 * @author vaclav
 */
public class ExportToODS {
    
    /**
     * Executes export to ODS format.
     * 
     * @param file file where the exported dvds and movies store to
     * @param dvds dvds to export
     * @param movies movies to export
     */
    public static void execute(File file, List<Dvd> dvds, List<Movie> movies) throws Exception {
        DvdManager dm = new DvdManagerImpl();
        MovieManager mm = new MovieManagerImpl();

        OdfSpreadsheetDocument newOdf = OdfSpreadsheetDocument.newSpreadsheetDocument();
        newOdf.getTableByName("Sheet1").remove();

        String[][] dvdsData = new String[dvds.size() + 1][3];
        dvdsData[0][0] = "Title";
        dvdsData[0][1] = "Type";
        dvdsData[0][2] = "Content";
        for (int i = 0; i < 3; i++) {
            for (int j = 1; j < dvds.size() + 1; j++) {
                switch (i) {
                    case 0:
                        dvdsData[j][0] = dvds.get(j-1).getTitle();
                        break;
                    case 1:
                        dvdsData[j][1] = dvds.get(j-1).getType().toString();
                        break;
                    case 2:
                        StringBuilder content = new StringBuilder();
                        
                        for (Movie m : dvds.get(j-1).getContent()) {
                            content.append(m.toString());
                            content.append(", ");
                        }
                        
                        int lastIndexOfComma = content.lastIndexOf(",");
                        if (lastIndexOfComma < 1) {
                            break;
                        }
                        
                        content.delete(lastIndexOfComma, lastIndexOfComma + 1);
                        dvdsData[j][2] = content.toString();
                        
                        break;
                }
            }
        }
       
        String[][] moviesData = new String[movies.size() + 1][6];
        moviesData[0][0] = "Name";
        moviesData[0][1] = "Director";
        moviesData[0][2] = "Year";
        moviesData[0][3] = "Country";
        moviesData[0][4] = "Actors";
        moviesData[0][5] = "Genres";
        for (int i = 0; i < 6; i++) {
            for (int j = 1; j < movies.size() + 1; j++) {
                switch (i) {
                    case 0:
                        moviesData[j][0] = movies.get(j-1).getName();
                        break;
                    case 1:
                        moviesData[j][1] = movies.get(j-1).getDirector();
                        break;
                    case 2:
                        moviesData[j][2] = movies.get(j-1).getYear().toString();
                        break;
                    case 3:
                        moviesData[j][3] = movies.get(j-1).getCountry();
                        break;
                    case 4:
                        StringBuilder content = new StringBuilder();
                        
                        for (String s : movies.get(j-1).getActors()) {
                            content.append(s);
                            content.append(", ");
                        }
                        
                        int lastIndexOfComma = content.lastIndexOf(",");
                        if (lastIndexOfComma < 1) {
                            break;
                        }
                        
                        content.delete(lastIndexOfComma, lastIndexOfComma + 1);
                        moviesData[j][4] = content.toString();
                        
                        break;
                    case 5:
                        StringBuilder content2 = new StringBuilder();
                        
                        for (Genre g : movies.get(j-1).getGenres()) {
                            content2.append(g.toString());
                            content2.append(", ");
                        }
                        
                        int lastIndexOfComma2 = content2.lastIndexOf(",");
                        if (lastIndexOfComma2 < 1) {
                            break;
                        }
                        
                        content2.delete(lastIndexOfComma2, lastIndexOfComma2 + 1);
                        moviesData[j][5] = content2.toString();
                        
                        break;
                }
            }
        }

        OdfTable dvdsTable = OdfTable.newTable(newOdf, null, null, dvdsData);
        OdfTable moviesTable = OdfTable.newTable(newOdf, null, null, moviesData);
        dvdsTable.setTableName("Dvds");
        dvdsTable.getCellByPosition(0, 0).setStringValue("Title");
        moviesTable.setTableName("Movies");
        moviesTable.getCellByPosition(0, 0).setStringValue("Name");

        newOdf.save(file);
    }
}
